﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Order
{
    public partial class ShipAndPay : System.Web.UI.Page
    {
        private readonly BLL.OrderBLL orderbll = new BLL.OrderBLL();
        private readonly BLL.ShipBLL shipbll = new BLL.ShipBLL();
        private readonly string username = HttpContext.Current.User.Identity.Name;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                createDate.Text = DateTime.Now.ToString();
                //绑定payMethod
                DataTable dtOrder = orderbll.getOrderByID(Session["orderid"].ToString());
                string cmdstr = "select * from PayMethod";
                SqlHelper.BindDropDownList(ddlPayMethod, "methodName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                //绑定shipMethod
                cmdstr = "select * from ShipMethod";
                SqlHelper.BindDropDownList(ddlShip, "methodName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                
                //如果finished=0则表示该订单中尚未包含该以下数据，需加载用户的默认数据
                if (!bool.Parse(dtOrder.Rows[0]["finished"].ToString()))
                {
                    BLL.UserBLL userbll = new BLL.UserBLL();
                    string payMethod = userbll.getPayMethod(username);
                    ddlPayMethod.SelectedValue = payMethod;
                    string shipMethod = userbll.getShipMethod(username);
                    ddlShip.SelectedValue = shipMethod;
                    //发货时间
                    lbShipDate.Text = shipbll.getReqShipDdate(DateTime.Parse(createDate.Text),Session["orderid"].ToString()).ToShortDateString();
                }
                else
                { 
                    BLL.InvoiceBLL invoicebll = new BLL.InvoiceBLL();
                    selectItem(ddlPayMethod,invoicebll.getInvoiceByNo(dtOrder.Rows[0]["invoiceNo"].ToString()).Rows[0]["payMethod"].ToString());
                    selectItem(ddlShip,dtOrder.Rows[0]["shipMethod"].ToString());
                    createDate.Text = dtOrder.Rows[0]["createDate"].ToString();
                    lbShipDate.Text = dtOrder.Rows[0]["requiredShipDate"].ToString();
                }
            }
        }

        //根据Item Text 初始化dropdownlist
        protected void selectItem(DropDownList ddl,string itemtext)
        {
            for(int i=0;i<ddl.Items.Count;i++)
            {
                if (ddl.Items[i].Text == itemtext)
                {
                    ddl.SelectedIndex = i;
                    break;
                }
            }
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            string orderid = Session["orderid"].ToString();
            Albon.Model.OrderInfo order = new Albon.Model.OrderInfo();
             //将临时数据更新为正式数据finished=1 ship invoice
            order.ID=int.Parse(orderid);
             //添加invoice数据
             BLL.InvoiceBLL invoicebll = new BLL.InvoiceBLL();
             Albon.Model.Invoice invoice = new Albon.Model.Invoice();
             invoice.invoiceNo =invoicebll.getInvoiceNo(orderid);
             invoice.payMethod = ddlPayMethod.SelectedItem.Text;
             invoicebll.alterInvoice(invoice);
            //获取requireShipDate
             
             order.invoiceNo=invoice.invoiceNo.ToString();
             order.requiredShipDate =DateTime.Parse(lbShipDate.Text);
             //ship method 
             order.shipMethod = ddlShip.SelectedItem.Text;
             order.createDate =DateTime.Parse(createDate.Text);
             orderbll.finishOrder(order);
             Response.Redirect("/Manager/OrderManager/OrderList.aspx");
        }
        
        
    }
}